package com.gobo.servicevod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.gobo.commonutils.R;
import com.gobo.servicevod.service.AliyunVodService;
import com.gobo.servicevod.util.AliyunVodSdkUtil;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
public class AliyunVodController {

    @Autowired
    private AliyunVodService vodService;

    @PostMapping("/upload")
    public R uploadVideo(MultipartFile file) {
        String s = vodService.uploadVideo(file);
        return s != null ? R.ok().data("videoId", s) : R.error();
    }
    @DeleteMapping("/delete/{videoId}")
    public R deleteVideo(@PathVariable String videoId){
        Boolean result = vodService.removeVideoByVideoId(videoId);
        return result?R.ok():R.error();
    }
    @DeleteMapping("/deleteList")
    public R deleteVideoList(@RequestParam List<String> videoSources){
        Boolean result = vodService.removeVideovideoSources(videoSources);
        return result?R.ok():R.error();
    }
    @GetMapping("/getvideoSourcesAuthor/{vidSourceId}")
    public R getvideoSourcesAuthorById(@PathVariable String vidSourceId) {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        try {
            DefaultAcsClient client = AliyunVodSdkUtil.initVodClient("LTAI5t8sbTLdW3zHGZPCorde", "bkZbT7KxeUbX1XgmaLkXbn1gZEixqS");
            request.setVideoId(vidSourceId);
            response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth",playAuth);
        } catch (ClientException e) {
            return R.error().message(e.getMessage());
        }
    }


}
